package com.android.support.lib.universal.view.dialog;

import com.android.support.lib.universal.R;
import com.android.support.lib.universal.listenner.ListennerLr;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 更改用户名弹框 使用说明 AlertDialogNickName alertDialogNickName=new
 * AlertDialogNickName(this); alertDialogNickName.setNePositiveButton即可
 * 
 * @author 彭治铭
 *
 */
public class AlertDialogNickName {
	Context context;
	Dialog dialog;
	EditText edit_nickname;// 文本编辑框
	Button button_Positive;
	Button button_Negative;
	Window window;

	/**
	 * 
	 * @param context
	 * @param bool
	 *            false(按对话框以外的地方不起作用,即不会消失)，true(按其他区域会消失)。按返回键还起作用
	 */
	@SuppressLint("NewApi")
	public AlertDialogNickName(Context context, Boolean bool) {
		super();
		// TODO Auto-generated constructor stub
		this.context = context;
		dialog = new Dialog(context, R.style.Dialog);// R.style.Dialog为布局主题，也可以用0
		dialog.show();
		dialog.setCanceledOnTouchOutside(bool);// 调用这个方法时，按对话框以外的地方不起作用。按返回键还起作用
		window = dialog.getWindow();
		window.setContentView(R.layout.lib_alertdialog_nickname);// 弹出框的布局
		initUI();
		listenner();
	}

	/**
	 * 设置按钮,保存
	 * 
	 * @param activity
	 *            用于更新Activity里面的昵称
	 * @param txt_name
	 *            更新该控件
	 * @param listener
	 *            成功回调，返回更新的文本数据 new ListennerLr()
	 */
	public void setNePositiveButton(final Activity activity,
			final TextView txt_name, final ListennerLr listener) {
		if (txt_name.getText() != null && txt_name.getText().length() >= 1) {
			edit_nickname.setText(txt_name.getText());// 给文本编辑框赋初始值
		}
		button_Positive.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (edit_nickname.getText() != null
						&& edit_nickname.getText().length() >= 1) {
					txt_name.setText(edit_nickname.getText());
					listener.OnResult(edit_nickname.getText().toString());// 成功回调
					dismiss();
				}
			}
		});
	}

	/**
	 * 关闭对话框
	 */
	public void dismiss() {
		dialog.dismiss();
	}


	protected void initUI() {
		// TODO Auto-generated method stub
		edit_nickname = (EditText) window.findViewById(R.id.edit_nickname);
		button_Negative = (Button) window.findViewById(R.id.button_Negative);
		button_Positive = (Button) window.findViewById(R.id.button_Positive);
	}

	protected void listenner() {
		// TODO Auto-generated method stub
		// 内置取消按钮事件
		button_Negative.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
	}

}
